package com.flash.dailyreport.controller;

import com.flash.dailyreport.entity.NewsEntity;
import com.flash.dailyreport.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sunyiming
 * @date 2018/8/17
 */
@Controller
@RequestMapping("news")
public class NewsController {

    @Autowired
    NewsService newsService;

    @RequestMapping("getNews")
    @ResponseBody
    public Map<String, List<NewsEntity>> getNews(String name) {
        Map<String, List<NewsEntity>> news = newsService.getNewsFrom(name);
        return news;
    }

    @RequestMapping("getSimpleNews")
    @ResponseBody
    public Map<String, List<String>> getSimpleNews(String name) {
        Map<String, List<String>> simpleNews = new HashMap<>();
        Map<String, List<NewsEntity>> news = newsService.getNewsFrom(name);

        for (Map.Entry<String, List<NewsEntity>> entry : news.entrySet()) {
            String key = entry.getKey();
            List<NewsEntity> newsEntities = entry.getValue();
            List<String> newsTitles = new ArrayList<>();
            for (NewsEntity newsEntity : newsEntities) {
                String time = newsEntity.getTime();
                String title = newsEntity.getTitle();
                newsTitles.add(String.format("[%s] %s", time, title));
            }
            simpleNews.put(key, newsTitles);
        }

        return simpleNews;
    }

}
